package com.hanscan.gui;

import com.hanscan.persistence.controllers.UserJpaController;
import com.hanscan.persistence.entities.User;
import com.hanscan.util.MD5;
import com.hanscan.util.language.LanguageConfig;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputSecret;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputLabel;
import com.icesoft.faces.component.ext.HtmlPanelGrid;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panellayout.PanelLayout;
import com.icesoft.faces.component.panelstack.PanelStack;
import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.event.ActionEvent;



public class ManagementClients extends AbstractFragmentBean
{

    private HtmlOutputLabel labelTittle = new HtmlOutputLabel();
    private HtmlCommandButton btnAdd = new HtmlCommandButton();
    private HtmlCommandButton btnEdit = new HtmlCommandButton();
    private HtmlCommandButton btnRemove = new HtmlCommandButton();
    private PanelStack panelStackMainMenu = new PanelStack();

    private HtmlOutputLabel labelMenuSeleccion = new HtmlOutputLabel();
    private HtmlOutputLabel labelName = new HtmlOutputLabel();
    private HtmlOutputLabel labelSurname1 = new HtmlOutputLabel();
    private HtmlOutputLabel labelSurname2 = new HtmlOutputLabel();
    private HtmlOutputLabel labelNumber = new HtmlOutputLabel();
    private HtmlOutputLabel labelNSP = new HtmlOutputLabel();
    private HtmlOutputLabel labelPhone = new HtmlOutputLabel();
    private HtmlOutputLabel labelMail = new HtmlOutputLabel();
    private HtmlOutputLabel labelStreetAndNumber = new HtmlOutputLabel();
    private HtmlOutputLabel labelZipCode = new HtmlOutputLabel();
    private HtmlOutputLabel labelProvince = new HtmlOutputLabel();
    private HtmlOutputLabel labelCity = new HtmlOutputLabel();
    private HtmlOutputLabel labelCountry = new HtmlOutputLabel();
    private HtmlOutputLabel labelUserLogin = new HtmlOutputLabel();
    private HtmlOutputLabel labelPassword = new HtmlOutputLabel();
    private HtmlOutputLabel labelConfirmPassword = new HtmlOutputLabel();
    private HtmlInputText inputTextName = new HtmlInputText();
    private HtmlInputText inputTextSurname1 = new HtmlInputText();
    private HtmlInputText inputTextSurname2 = new HtmlInputText();
    private HtmlInputText inputTextPhone = new HtmlInputText();
    private HtmlInputText inputTextMail = new HtmlInputText();
    private HtmlInputText inputTextStreetAndNumber = new HtmlInputText();
    private HtmlInputText inputTextZipCode = new HtmlInputText();
    private HtmlInputText inputTextProvince = new HtmlInputText();
    private HtmlInputText inputTextCity = new HtmlInputText();
    private HtmlInputText inputTextCountry = new HtmlInputText();
    private HtmlInputText inputTextUserLogin = new HtmlInputText();
    private HtmlInputSecret inputTextPassword = new HtmlInputSecret();
    private HtmlInputSecret inputTextConfirmPassword = new HtmlInputSecret();
    private HtmlCommandButton btnCreateUser = new HtmlCommandButton();
    private HtmlCommandButton btnEditUser = new HtmlCommandButton();
    private HtmlCommandButton btnRemoveUser = new HtmlCommandButton();
    private Boolean resetPassword;
    private HtmlOutputLabel labelResetPassword = new HtmlOutputLabel();
    private String colorEmptyField = "background-color:rgba(255, 0, 0, 0.5);";
    private Boolean renderEdit = false;
    private Boolean renderCreate = false;
    private Boolean renderRemove = false;
    //filtros
    private HtmlPanelGrid containerFilter = new HtmlPanelGrid();
    private HtmlInputText inputTextFilterByName = new HtmlInputText();
    private HtmlInputText inputTextFilterBySurname1 = new HtmlInputText();
    private HtmlInputText inputTextFilterBySurname2 = new HtmlInputText();
    private HtmlInputText inputTextFilterByNSP = new HtmlInputText();
    private HtmlOutputLabel labelFilterByName = new HtmlOutputLabel();
    private HtmlOutputLabel labelFilterSurname1 = new HtmlOutputLabel();
    private HtmlOutputLabel labelFilterSurname2 = new HtmlOutputLabel();
    private HtmlOutputLabel labelFilterByNumber = new HtmlOutputLabel();
    private PanelLayout panelEmpty11 = new PanelLayout();
    private PanelLayout panelEmpty12 = new PanelLayout();
    private HtmlCommandButton btnSearchUser = new HtmlCommandButton();
    private List<User> userList = new ArrayList();
    private User[] users;
    private HtmlPanelGrid containerSearch = new HtmlPanelGrid();
    private String sortColumnName;
    // flat to indicate multiselect row enabled.
    private boolean multiRowSelect;
    private boolean ascending;

    // we only want to resort if the oder or column has changed.
    private String oldSort;
    private boolean oldAscending;
    private String clientNameHeader;
    private String surnameHeader;
    private String idNumberHeader;
    private User currentUser = new User();
    private boolean nspView;


    private void _init() throws Exception
    {
        nspView = false;
        loadLanguage(getSessionBean1().getSessionBeanLanguage());
        SortableList(idNumberHeader);
        loadTable(new ArrayList<User>());
    }
    private SelectInputDateBean selectInputDate1Bean = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDate1Bean() {
        return selectInputDate1Bean;
    }

    public void setSelectInputDate1Bean(SelectInputDateBean sidb) {
        this.selectInputDate1Bean = sidb;
    }


    public ManagementClients()
    {
    }

    protected void loadLanguage(LanguageConfig languageConfig)
    {
        labelTittle.setValue(languageConfig.getProperty("Gestion_Clientes"));
        btnAdd.setValue(languageConfig.getProperty("Nuevo"));
        btnEdit.setValue(languageConfig.getProperty("Modificar"));
        btnRemove.setValue(languageConfig.getProperty("Suprimir"));

        labelName.setValue(languageConfig.getProperty("Nombre") + " " + "*");
        labelSurname1.setValue(languageConfig.getProperty("Primer_Apellido") +
                " " + "*");
        labelSurname2.setValue(languageConfig.getProperty("Segundo_Apellido"));
        labelNumber.setValue(languageConfig.getProperty("N_Usuario") + " " + "*");
        labelPhone.setValue(languageConfig.getProperty("Telefono1"));
        labelMail.setValue(languageConfig.getProperty("Email1"));
        labelStreetAndNumber.setValue(languageConfig.getProperty("Calle"));
        labelZipCode.setValue(languageConfig.getProperty("C_Postal"));
        labelProvince.setValue(languageConfig.getProperty("Provincia"));
        labelCity.setValue(languageConfig.getProperty("Ciudad"));
        labelCountry.setValue(languageConfig.getProperty("Pais"));
        labelUserLogin.setValue(languageConfig.getProperty("Nombre_De_Usuario"));
        labelPassword.setValue(languageConfig.getProperty("Password"));
        labelConfirmPassword.setValue(languageConfig.getProperty("Confirm_Password"));
        btnCreateUser.setValue(languageConfig.getProperty("Crear"));
        btnEditUser.setValue(languageConfig.getProperty("Editar"));
        btnRemoveUser.setValue(languageConfig.getProperty("Borrar"));
        btnSearchUser.setValue(languageConfig.getProperty("Buscar"));
        labelFilterByName.setValue(languageConfig.getProperty("Nombre"));
        labelFilterSurname1.setValue(languageConfig.getProperty("Primer_Apellido"));
        labelFilterSurname2.setValue(languageConfig.getProperty("Segundo_Apellido"));
        labelFilterByNumber.setValue(languageConfig.getProperty("N_Usuario"));

        //tablas
        clientNameHeader = languageConfig.getProperty("Nombre");
        surnameHeader = languageConfig.getProperty("Apellido");
        idNumberHeader = languageConfig.getProperty("Msj_Numero_ID");


    }

    public void setViewInit()
    {
    }

    public String btnAdd_action()
    {
        labelMenuSeleccion.setValue("Añadir");
        renderCreate = true;
        renderEdit = false;
        renderRemove = false;
        /******************************************/
        /*Mostrar/Ocultar PANELES correspondientes*/
        containerFilter.setRendered(false);
        containerSearch.setRendered(false);

        /******************************************/
        /*Mostrar/ocultar subtitulos*/
        labelPassword.setRendered(true);
        labelResetPassword.setRendered(false);
//                inputTextPassword.setRendered(true);
//                labelConfirmPassword.setRendered(true);
//                inputTextConfirmPassword.setRendered(true);
        lockFieldsEmployee(false);
        return null;
    }

    public String btnRemove_action(ActionEvent e)
    {
        labelMenuSeleccion.setValue("Borrar");
        renderCreate = false;
        renderEdit = false;
        renderRemove = true;
        lockFieldsEmployee(true);
        containerFilter.setRendered(true);
        containerSearch.setRendered(true);
        loadTable(new ArrayList<User>());
        SortableList(idNumberHeader);
        return null;
    }

    public String btnEdit_action(ActionEvent e)
    {
        labelMenuSeleccion.setValue("Editar");
        renderCreate = false;
        renderEdit = true;
        renderRemove = false;
        lockFieldsEmployee(false);
        containerFilter.setRendered(true);
        containerSearch.setRendered(true);
        loadTable(new ArrayList<User>());
        SortableList(idNumberHeader);
        return null;
    }

    public void lockFieldsEmployee(Boolean disabled)
    {
        inputTextName.setDisabled(disabled);
        inputTextSurname1.setDisabled(disabled);
        inputTextSurname2.setDisabled(disabled);
        inputTextPhone.setDisabled(disabled);
        inputTextMail.setDisabled(disabled);
        inputTextStreetAndNumber.setDisabled(disabled);
        inputTextZipCode.setDisabled(disabled);
        inputTextProvince.setDisabled(disabled);
        inputTextCity.setDisabled(disabled);
        inputTextCountry.setDisabled(disabled);
        inputTextUserLogin.setDisabled(disabled);
        inputTextPassword.setDisabled(disabled);
        inputTextConfirmPassword.setDisabled(disabled);
    }

    public void resetValues()
    {
        inputTextName.resetValue();
        inputTextSurname1.resetValue();
        inputTextSurname2.resetValue();
        inputTextPhone.resetValue();
        inputTextMail.resetValue();
        inputTextStreetAndNumber.resetValue();
        inputTextZipCode.resetValue();
        inputTextProvince.resetValue();
        inputTextCity.resetValue();
        inputTextCountry.resetValue();
        inputTextUserLogin.resetValue();
        inputTextPassword.resetValue();
        inputTextConfirmPassword.resetValue();
    }

    /**
     *
     * @param e
     * @return
     */
    public String btnCreateUser_action(ActionEvent e)
    {
        User userToCreate = addUser();

        if (userToCreate == null)
        {
            return null;
        }

        //Confirmacion de contraseña
        String passwd = (String) inputTextPassword.getValue();
        String confirmPasswd = (String) inputTextConfirmPassword.getValue();
        initColorInputSecret(inputTextPassword);
        initColorInputSecret(inputTextConfirmPassword);
        if ((!passwd.equals("")) || (!confirmPasswd.equals("")))
        {
            if (passwd.equals(confirmPasswd))
            {
                /* Ciframos la contraseña */
                byte[] pass = MD5.getHashMD5(passwd, null);
                userToCreate.setClientPassword(MD5.toHexadecimal(pass));
            } 
            else
            {
                errorColorInputSecret(inputTextPassword);
                errorColorInputSecret(inputTextConfirmPassword);

//                getMainMenu().setMessageHeader(popUpHeaderError);
//                getMainMenu().setMessageText(popUpMessageErrorPasswordDontMatch);
//                getMainMenu().getPanelPopup1Bean().setShowModalPanel(true);
                return null;
            }
        }

        // Guardamos en BBDd
//        clientFacade.create(clientToCreate);
//        clearColorFields();
//        resetValues();
//        getMainMenu().setMessageHeader(popUpHeaderOk);
//        getMainMenu().setMessageText(popUpHeaderOk);
//        getMainMenu().getPanelPopup1Bean().setShowModalPanel(true);

        return null;
    }

    public String btnSearchUser_action(ActionEvent e)
    {
        containerFilter.setRendered(true);


        List<User> userLists = new ArrayList();

        User user = new User();

        user.setName(inputTextFilterByName.getText());
        user.setSurname1(inputTextFilterBySurname1.getText());
        user.setSurname2(inputTextFilterBySurname2.getText());

        try
        {
           UserJpaController userJpaController = new UserJpaController();
           userLists = userJpaController.findUser(user);
        } 
        catch (Exception ex)
        {
            //Todo: no se ha podido realizar la busqueda, comprobar detalles.
//            mainMenuBean.setPanelPopUp(popUpHeaderError, popUpMessageError, popUpvisible);
        }


        if (userList.size() == 0)
        {
            //TODO: No hay ningun usuario con ese criterio. -- Cambiar mensaje.--
            loadTable();
//            mainMenuBean.setPanelPopUp(popUpHeaderError,popUpMessageError, popUpvisible);
        } else
        {
            loadTable(userLists);
//            outputTextNoneExistingUser.setValue("");
        }
        return null;
    }

    /*Table*/
    public void loadTable()
    {
        try
        {
            UserJpaController userJpaController = new UserJpaController();
            userList = userJpaController.findUserEntities();
        } 
        catch (Exception ex)
        {
            ex.printStackTrace();
//            mainMenuBean.setPanelPopUp(popUpHeaderError,popUpMessageError, popUpvisible);
//            outputTextNoneExistingUser.setValue("Error al recuperar los usuarios");
//            panelPopup1Bean.setShowModalPanel(true);
//            panelPopup1Bean.setShowDraggablePanel(true);
//            getSessionBean1().setMessageText("Error al recuperar los usuarios");
//            getSessionBean1().setMessageHeader("Advertencia");
        }
        users = new User[userList.size()];
        for (int i = 0; i < userList.size(); i++)
        {
            users[i] = userList.get(i);
        }
    }

    public void loadTable(List<User> usersLista)
    {
        users = new User[usersLista.size()];
        for (int i = 0; i < usersLista.size(); i++)
        {
            users[i] = usersLista.get(i);
        }
    }

    public void rowSelectionListener(RowSelectorEvent event)
    {
        User user = new User();
        for (int i = 0, max = users.length; i < max; i++)
        {

            if (i == event.getRow())
            {
                try
                {
                    UserJpaController userJpaController = new UserJpaController();
                    user = (User) userJpaController.findUserById(users[i].getUserId());
                } 
                catch (Exception ex)
                {
                    ex.printStackTrace();
//                    mainMenuBean.setPanelPopUp(popUpHeaderError, popUpMessageError, popUpvisible);
                }
            }
        }

        containerSearch.setRendered(false);
        containerFilter.setRendered(false);
        currentUser = user;
        fillFieldsUser(user);
    }

    public void fillFieldsUser(User user)
    {
        inputTextName.setValue(user.getName());
        inputTextSurname1.setValue(user.getSurname1());
        inputTextSurname2.setValue(user.getSurname2());
        inputTextPhone.setValue(user.getTelephone());
        inputTextMail.setValue(user.getEmail());
        inputTextStreetAndNumber.setValue(user.getAddress());
        inputTextZipCode.setValue(user.getZipCode());
        inputTextProvince.setValue(user.getProvince());
        inputTextCity.setValue(user.getCity());
        inputTextCountry.setValue(user.getCountry());
        inputTextUserLogin.setValue(user.getClientLogin());
    }

    public User[] getClients()
    {
        if (!oldSort.equals(sortColumnName) || oldAscending != ascending) {
            sort();
            oldSort = sortColumnName;
            oldAscending = ascending;
        }
        return users;
    }

    public void setClients(User[] clients)
    {
        this.users = clients;
    }

    protected void sort()
    {
        Comparator comparator = new Comparator()
        {
            public int compare(Object o1, Object o2)
            {
                User c1 = (User) o1;
                User c2 = (User) o2;
                if (sortColumnName == null)
                {
                    return 0;
                } 
                else if (sortColumnName.equals(clientNameHeader))
                {
                    return ascending ? c1.getName().compareTo(c2.getName()) : c2.getName().
                            compareTo(c1.getName());
                } 
                else if (sortColumnName.equals(surnameHeader))
                {
                    return ascending ? c1.getSurname1().compareTo(
                            c2.getSurname1()) : c2.getSurname1().compareTo(c1.getSurname1());
                } else
                {
                    return 0;
                }
            }
        };
        Arrays.sort(users, comparator);

    }

    private void SortableList(String defaultSortColumn)
    {
        sortColumnName = defaultSortColumn;
        ascending = isDefaultAscending(defaultSortColumn);
        oldSort = sortColumnName;
        // make sure sortColumnName on first render
        oldAscending = !ascending;
    }

    public boolean isDefaultAscending(String sortColumn)
    {
        return true;
    }

    /**
     *
     * @param inputText
     */
    public void changeColorInputText(HtmlInputText inputText)
    {
        if (inputText.getText().equals(""))
        {
            inputText.setStyle(colorEmptyField);
        }
        else
        {
            inputText.setStyle("");
        }
    }

    /**
     *
     * @param inputText
     */
    public void initColorInputText(HtmlInputText inputText)
    {
        inputText.setStyle("");
    }

    /**
     *
     * @param inputText
     */
    public void errorColorInputText(HtmlInputText inputText)
    {
        inputText.setStyle(colorEmptyField);
    }

    /**
     *
     * @param inputSecret
     */
    public void initColorInputSecret(HtmlInputSecret inputSecret)
    {
        inputSecret.setStyle("");
    }

    /**
     *
     * @param inputSecret
     */
    public void errorColorInputSecret(HtmlInputSecret inputSecret)
    {
        inputSecret.setStyle(colorEmptyField);
    }

    /**
     *
     * @return
     */
    public User addUser()
    {
        User user = new User();

        initColorInputText(inputTextName);
        initColorInputText(inputTextSurname1);

        if ((inputTextName.getText().equals("")) ||
                (inputTextSurname1.getText().equals("")))
        {
            changeColorInputText(inputTextName);
            changeColorInputText(inputTextSurname1);
//            getMainMenu().setMessageHeader(popUpHeaderError);
//            getMainMenu().setMessageText(popUpMessageErrorNullAtribute);
//            getMainMenu().getPanelPopup1Bean().setShowModalPanel(true);
            return null;
        }

        user.setName(String.valueOf(inputTextName.getText()));
        user.setSurname1(String.valueOf(inputTextSurname1.getValue()));
        user.setSurname2(String.valueOf(inputTextSurname2.getValue()));
        user.setTelephone(String.valueOf(inputTextPhone.getValue()));
        user.setEmail(String.valueOf(inputTextMail.getValue()));
        user.setAddress(String.valueOf(inputTextStreetAndNumber.getValue()));
        user.setZipCode(String.valueOf(inputTextZipCode.getValue()));
        user.setProvince(String.valueOf(inputTextProvince.getValue()));
        user.setCity(String.valueOf(inputTextCity.getValue()));
        user.setCountry(String.valueOf(inputTextCountry.getValue()));
        user.setClientLogin(inputTextUserLogin.getText());
        user.setClientPassword(String.valueOf(inputTextPassword.getValue()));
        try
        {
           
        } 
        catch (NumberFormatException ex)
        {
            user = null;
            ex.printStackTrace();
//            getMainMenu().setMessageHeader(popUpHeaderError);
//            getMainMenu().getPanelPopup1Bean().setShowModalPanel(true);
            return null;
        }

//TODO YA esta el usuario
//        if (existingUser != null)
//        {
//            System.out.println(" existingClient es " + existingUser.getName() + " y " + existingClient.getSurname1());
//            System.out.println("Id number repetido");
//            user=null;
//            getMainMenu().setMessageHeader(popUpHeaderError);
//            getMainMenu().setMessageText(popUpMessageErrorNumClienteRepetido);
//            getMainMenu().getPanelPopup1Bean().setShowModalPanel(true);
//        }

        return user;
    }


    
    public String btnEditUser_action(ActionEvent e)
    {
        User userEdit = new User();
//        /*Temporal*/
////        employee.setUserId(Long.parseLong(inputTextIdUser.getValue().toString()));
//        /**********/
        userEdit.setUserId(currentUser.getUserId());
        userEdit.setBioTemplates(currentUser.getBioTemplates());
        userEdit = fillClient(userEdit);
        Boolean errorPasswords = false;
        if ((!inputTextPassword.getValue().equals("")) && (!inputTextConfirmPassword.getValue().
                equals("")))
        {
            String password = (String) inputTextPassword.getValue();
            String passwordConfirm =
                    (String) inputTextConfirmPassword.getValue();
            if (password.equals(passwordConfirm))
            {
                byte[] pass = MD5.getHashMD5(password, null);
                userEdit.setClientPassword(MD5.toHexadecimal(pass));

            } else
            {
                //TODO: error passwords no coinciden
                errorPasswords = true;
            }
        }
        else
        {
            userEdit.setClientPassword(currentUser.getClientPassword());
        }
        changeColorEmployeeFields();

        if (!errorPasswords)
        {
            try
            {
                UserJpaController userJpaController = new UserJpaController();
                userJpaController.edit(userEdit);
//                clearColorFields();
//                resetValues();
//                mainMenuBean.setPanelPopUp(popUpHeaderOk,popUpMessageOkEdit, popUpvisible);
            } 
//            catch (NonexistentEntityException ex)
//            {
//                ex.printStackTrace();
//                mainMenuBean.setPanelPopUp(popUpHeaderError, popUpMessageError, popUpvisible);
//            }
//            catch (PreexistingEntityException pee)
//            {
//                inputTextUserLogin.setValue("");
//                pee.printStackTrace();
//                mainMenuBean.setPanelPopUp(popUpHeaderError,popUpMessageErrorPreexistingUserLogin, popUpvisible);
//            }
            catch (Exception ex)
            {
                ex.printStackTrace();
//                mainMenuBean.setPanelPopUp(popUpHeaderError,popUpMessageError, popUpvisible);
            }
        }
        else
        {
//            mainMenuBean.setPanelPopUp(popUpHeaderError,popUpMessageErrorPasswordDontMatch, popUpvisible);
        }

        return null;
    }

    public void changeColorEmployeeFields()
    {
        changeColorInputText(inputTextName);
        changeColorInputText(inputTextSurname1);
    }

     public void clearColorFields()
    {
        clearStyleInputText(inputTextName);
        clearStyleInputText(inputTextSurname1);
     }



    public void clearStyleInputText(HtmlInputText inputText)
    {
        inputText.setStyle("");
    }

    public void clearStyleInputSecret(HtmlInputSecret inputSecret)
    {
        inputSecret.setStyle("");
    }

    public String btnRemoveUser_action(ActionEvent e)
    {
//         if (currentEmployee.getTerminals() != null)
//        {
//            if (currentEmployee.getTerminals().size() != 0)
//            {
                try
                {
                    //TODO: Mensaje si no ha podido borrar las huellas del terminal
//                    deletePatternsInTerminals(currentEmployee);
//                    if (terminalDeleteFailed != null)
//                    {
//                        if (terminalDeleteFailed.size() == 0)
//                        {
                            //No ha fallado el borrado de huella de ningún terminal
//                            clientFacade.remove(currentEmployee);
//                            setView();
//                            clearColorFields();
//                            resetValues();
//                            mainMenuBean.setPanelPopUp(popUpHeaderOk,
//                                    popUpMessageOkRemove, popUpvisible);
//                            System.out.println(
//                                    "-------------No ha fallado el borrado de huella de ningún terminal");

//                        } else
//                        {
//                            //TODO: Mensaje de que el usuario no se ha podido eliminar
//                            //Alguna huella no ha podido ser eliminada de algún terminal
//                            System.out.println(
//                                    "-------------Alguna huella no ha podido ser eliminada de algún terminal");
//                        }
//                    }
//                }
//                            catch (HanscanCommunicationException ex)
//                {
//                    //Mensaje si no ha podido borrar las huellas del terminal
//                    ex.printStackTrace();
//                    Logger.getLogger(ManagementClients.class.getName()).
//                            log(Level.SEVERE, null, ex);
//                    mainMenuBean.setPanelPopUp(popUpHeaderError,
//                            popUpMessageErrorHanscanCommunicationException,
//                            popUpvisible);
//                } catch (HanscanProtocolException ex)
//                {
//                    //Mensaje si no ha podido borrar las huellas del terminal
//                    ex.printStackTrace();
//                    Logger.getLogger(ManagementClients.class.getName()).
//                            log(Level.SEVERE, null, ex);
//                    mainMenuBean.setPanelPopUp(popUpHeaderError,
//                            popUpMessageError, popUpvisible);
//                } catch (IncompleteOperationException ex)
//                {
//                    //Mensaje si no ha podido borrar las huellas del terminal
//                    ex.printStackTrace();
//                    Logger.getLogger(ManagementClients.class.getName()).
//                            log(Level.SEVERE, null, ex);
//                    mainMenuBean.setPanelPopUp(popUpHeaderError,
//                            popUpMessageError, popUpvisible);
//                }
//                catch (NonexistentEntityException nee)
//                {
//                    //Mensaje si no ha podido borrar el usuario de BBDD
//                    nee.printStackTrace();
//                    Logger.getLogger(ManagementClients.class.getName()).
//                            log(Level.SEVERE, null, nee);
//                    mainMenuBean.setPanelPopUp(popUpHeaderError,
//                            popUpMessageError, popUpvisible);
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
//                    mainMenuBean.setPanelPopUp(popUpHeaderError,popUpMessageError, popUpvisible);
                }
//            } else
//            {
//                deleteEmployee(currentEmployee);
//            }
//        } else
//        {
//            deleteEmployee(currentEmployee);
//        }


        return null;
    }

     private void deletePatternsInTerminals(User user)
//            throws HanscanCommunicationException, HanscanProtocolException,
//            IncompleteOperationException, Exception
    {
//        try
//        {
//            deleteAll(employee);
//
//        } catch (HanscanCommunicationException ex)
//        {
//            ex.printStackTrace();
//            Logger.getLogger(ManagementClients.class.getName()).log(Level.SEVERE,
//                    null, ex);
//            throw ex;//TODO
//        } catch (HanscanProtocolException ex)
//        {
//            ex.printStackTrace();
//            Logger.getLogger(ManagementClients.class.getName()).log(Level.SEVERE,
//                    null, ex);
//            throw ex;//TODO
//        } catch (IncompleteOperationException ex)
//        {
//            ex.printStackTrace();
//            Logger.getLogger(ManagementClients.class.getName()).log(Level.SEVERE,
//                    null, ex);
//            throw ex;//TODO
//        }
//        catch(Exception ex)
//        {
//            ex.printStackTrace();
//            Logger.getLogger(ManagementClients.class.getName()).log(Level.SEVERE,
//                    null, ex);
//            throw ex;//TODO
//        }
    }

    public User fillClient(User user)
    {
//        List<BioTemplateTransfer> bioTemplateTransfers =
//                new ArrayList<BioTemplateTransfer>();
//        List<BioTemplate> bioTemplates = new ArrayList<BioTemplate>();

        if (!inputTextName.getText().equals("")) {
            user.setName(inputTextName.getText());
        }

        if (!inputTextSurname1.getText().equals("")) {
            user.setSurname1(inputTextSurname1.getText());
        }
        user.setSurname2(inputTextSurname2.getText());
        user.setTelephone(inputTextPhone.getText());
        user.setEmail(inputTextMail.getText());
        user.setAddress(inputTextStreetAndNumber.getText());
        user.setZipCode(inputTextZipCode.getText());
        user.setProvince(inputTextProvince.getText());
        user.setCity(inputTextCity.getText());
        user.setCountry(inputTextCountry.getText());
        if (!inputTextUserLogin.getText().equals(""))
        {
            user.setClientLogin(inputTextUserLogin.getText());
        }
        return user;
    }


    @Override
    public void destroy()
    {
    }

    @Override
    public void init()
    {
        super.init();
        try
        {
            _init();
        }
        catch (Exception e)
        {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }
    }

    //////////////////////////////////////////////////////////////////
    //getter y setter
    //////////////////////////////////////////////////////////////////
    private MainMenu getMainMenu() {
        MainMenu mainMenu = (MainMenu) getBean("MainMenu");
        return mainMenu;
    }

    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public HtmlCommandButton getBtnAdd() {
        return btnAdd;
    }

    public void setBtnAdd(HtmlCommandButton btnAdd) {
        this.btnAdd = btnAdd;
    }

    public HtmlCommandButton getBtnEdit() {
        return btnEdit;
    }

    public void setBtnEdit(HtmlCommandButton btnEdit) {
        this.btnEdit = btnEdit;
    }

    public HtmlCommandButton getBtnRemove() {
        return btnRemove;
    }

    public void setBtnRemove(HtmlCommandButton btnRemove) {
        this.btnRemove = btnRemove;
    }

    public HtmlCommandButton getBtnCreateUser() {
        return btnCreateUser;
    }

    public void setBtnCreateUser(HtmlCommandButton btnCreateUser) {
        this.btnCreateUser = btnCreateUser;
    }

    public HtmlCommandButton getBtnEditUser() {
        return btnEditUser;
    }

    public void setBtnEditUser(HtmlCommandButton btnEditUser) {
        this.btnEditUser = btnEditUser;
    }

    public HtmlCommandButton getBtnRemoveUser() {
        return btnRemoveUser;
    }

    public void setBtnRemoveUser(HtmlCommandButton btnRemoveUser) {
        this.btnRemoveUser = btnRemoveUser;
    }

    public HtmlInputText getInputTextCity() {
        return inputTextCity;
    }

    public void setInputTextCity(HtmlInputText inputTextCity) {
        this.inputTextCity = inputTextCity;
    }

    public HtmlInputSecret getInputTextConfirmPassword() {
        return inputTextConfirmPassword;
    }

    public void setInputTextConfirmPassword(HtmlInputSecret inputTextConfirmPassword) {
        this.inputTextConfirmPassword = inputTextConfirmPassword;
    }

    public HtmlInputText getInputTextCountry() {
        return inputTextCountry;
    }

    public void setInputTextCountry(HtmlInputText inputTextCountry) {
        this.inputTextCountry = inputTextCountry;
    }

    public HtmlInputText getInputTextName() {
        return inputTextName;
    }

    public void setInputTextName(HtmlInputText inputTextName) {
        this.inputTextName = inputTextName;
    }

    public HtmlInputSecret getInputTextPassword() {
        return inputTextPassword;
    }

    public void setInputTextPassword(HtmlInputSecret inputTextPassword) {
        this.inputTextPassword = inputTextPassword;
    }

    public HtmlInputText getInputTextProvince() {
        return inputTextProvince;
    }

    public void setInputTextProvince(HtmlInputText inputTextProvince) {
        this.inputTextProvince = inputTextProvince;
    }

    public HtmlInputText getInputTextStreetAndNumber() {
        return inputTextStreetAndNumber;
    }

    public void setInputTextStreetAndNumber(HtmlInputText inputTextStreetAndNumber) {
        this.inputTextStreetAndNumber = inputTextStreetAndNumber;
    }

    public HtmlInputText getInputTextSurname1() {
        return inputTextSurname1;
    }

    public void setInputTextSurname1(HtmlInputText inputTextSurname1) {
        this.inputTextSurname1 = inputTextSurname1;
    }

    public HtmlInputText getInputTextSurname2() {
        return inputTextSurname2;
    }

    public void setInputTextSurname2(HtmlInputText inputTextSurname2) {
        this.inputTextSurname2 = inputTextSurname2;
    }

    public HtmlInputText getInputTextUserLogin() {
        return inputTextUserLogin;
    }

    public void setInputTextUserLogin(HtmlInputText inputTextUserLogin) {
        this.inputTextUserLogin = inputTextUserLogin;
    }

    public HtmlInputText getInputTextZipCode() {
        return inputTextZipCode;
    }

    public void setInputTextZipCode(HtmlInputText inputTextZipCode) {
        this.inputTextZipCode = inputTextZipCode;
    }

    public HtmlOutputLabel getLabelCity() {
        return labelCity;
    }

    public void setLabelCity(HtmlOutputLabel labelCity) {
        this.labelCity = labelCity;
    }

    public HtmlOutputLabel getLabelConfirmPassword() {
        return labelConfirmPassword;
    }

    public void setLabelConfirmPassword(HtmlOutputLabel labelConfirmPassword) {
        this.labelConfirmPassword = labelConfirmPassword;
    }

    public HtmlOutputLabel getLabelCountry() {
        return labelCountry;
    }

    public void setLabelCountry(HtmlOutputLabel labelCountry) {
        this.labelCountry = labelCountry;
    }

    public HtmlOutputLabel getLabelName() {
        return labelName;
    }

    public void setLabelName(HtmlOutputLabel labelName) {
        this.labelName = labelName;
    }

    public HtmlOutputLabel getLabelNumber() {
        return labelNumber;
    }

    public void setLabelNumber(HtmlOutputLabel labelNumber) {
        this.labelNumber = labelNumber;
    }

    public HtmlOutputLabel getLabelPassword() {
        return labelPassword;
    }

    public void setLabelPassword(HtmlOutputLabel labelPassword) {
        this.labelPassword = labelPassword;
    }

    public HtmlOutputLabel getLabelProvince() {
        return labelProvince;
    }

    public void setLabelProvince(HtmlOutputLabel labelProvince) {
        this.labelProvince = labelProvince;
    }

    public HtmlOutputLabel getLabelStreetAndNumber() {
        return labelStreetAndNumber;
    }

    public void setLabelStreetAndNumber(HtmlOutputLabel labelStreetAndNumber) {
        this.labelStreetAndNumber = labelStreetAndNumber;
    }

    public HtmlOutputLabel getLabelSurname1() {
        return labelSurname1;
    }

    public void setLabelSurname1(HtmlOutputLabel labelSurname1) {
        this.labelSurname1 = labelSurname1;
    }

    public HtmlOutputLabel getLabelSurname2() {
        return labelSurname2;
    }

    public void setLabelSurname2(HtmlOutputLabel labelSurname2) {
        this.labelSurname2 = labelSurname2;
    }

    public HtmlOutputLabel getLabelTittle() {
        return labelTittle;
    }

    public void setLabelTittle(HtmlOutputLabel labelTittle) {
        this.labelTittle = labelTittle;
    }

    public HtmlOutputLabel getLabelUserLogin() {
        return labelUserLogin;
    }

    public void setLabelUserLogin(HtmlOutputLabel labelUserLogin) {
        this.labelUserLogin = labelUserLogin;
    }

    public HtmlOutputLabel getLabelZipCode() {
        return labelZipCode;
    }

    public void setLabelZipCode(HtmlOutputLabel labelZipCode) {
        this.labelZipCode = labelZipCode;
    }

    public HtmlOutputLabel getLabelResetPassword() {
        return labelResetPassword;
    }

    public void setLabelResetPassword(HtmlOutputLabel labelResetPassword) {
        this.labelResetPassword = labelResetPassword;
    }

    public Boolean getResetPassword() {
        return resetPassword;
    }

    public void setResetPassword(Boolean resetPassword) {
        this.resetPassword = resetPassword;
    }

    public Boolean getRenderCreate() {
        return renderCreate;
    }

    public void setRenderCreate(Boolean renderCreate) {
        this.renderCreate = renderCreate;
    }

    public Boolean getRenderEdit() {
        return renderEdit;
    }

    public void setRenderEdit(Boolean renderEdit) {
        this.renderEdit = renderEdit;
    }

    public Boolean getRenderRemove() {
        return renderRemove;
    }

    public void setRenderRemove(Boolean renderRemove) {
        this.renderRemove = renderRemove;
    }

    public HtmlPanelGrid getContainerFilter() {
        return containerFilter;
    }

    public void setContainerFilter(HtmlPanelGrid containerFilter) {
        this.containerFilter = containerFilter;
    }

    public HtmlInputText getInputTextFilterByName() {
        return inputTextFilterByName;
    }

    public void setInputTextFilterByName(HtmlInputText inputTextFilterByName) {
        this.inputTextFilterByName = inputTextFilterByName;
    }

    public HtmlInputText getInputTextFilterBySurname1() {
        return inputTextFilterBySurname1;
    }

    public void setInputTextFilterBySurname1(HtmlInputText inputTextFilterBySurname1) {
        this.inputTextFilterBySurname1 = inputTextFilterBySurname1;
    }

    public HtmlInputText getInputTextFilterBySurname2() {
        return inputTextFilterBySurname2;
    }

    public void setInputTextFilterBySurname2(HtmlInputText inputTextFilterBySurname2) {
        this.inputTextFilterBySurname2 = inputTextFilterBySurname2;
    }

    public HtmlOutputLabel getLabelFilterByName() {
        return labelFilterByName;
    }

    public void setLabelFilterByName(HtmlOutputLabel labelFilterByName) {
        this.labelFilterByName = labelFilterByName;
    }

    public HtmlOutputLabel getLabelFilterByNumber() {
        return labelFilterByNumber;
    }

    public void setLabelFilterByNumber(HtmlOutputLabel labelFilterByNumber) {
        this.labelFilterByNumber = labelFilterByNumber;
    }

    public HtmlOutputLabel getLabelFilterSurname1() {
        return labelFilterSurname1;
    }

    public void setLabelFilterSurname1(HtmlOutputLabel labelFilterSurname1) {
        this.labelFilterSurname1 = labelFilterSurname1;
    }

    public HtmlOutputLabel getLabelFilterSurname2() {
        return labelFilterSurname2;
    }

    public void setLabelFilterSurname2(HtmlOutputLabel labelFilterSurname2) {
        this.labelFilterSurname2 = labelFilterSurname2;
    }

    public PanelLayout getPanelEmpty11() {
        return panelEmpty11;
    }

    public void setPanelEmpty11(PanelLayout panelEmpty11) {
        this.panelEmpty11 = panelEmpty11;
    }

    public PanelLayout getPanelEmpty12() {
        return panelEmpty12;
    }

    public void setPanelEmpty12(PanelLayout panelEmpty12) {
        this.panelEmpty12 = panelEmpty12;
    }

    public HtmlCommandButton getBtnSearchUser() {
        return btnSearchUser;
    }

    public void setBtnSearchUser(HtmlCommandButton btnSearchUser) {
        this.btnSearchUser = btnSearchUser;
    }

    public boolean isAscending() {
        return ascending;
    }

    public void setAscending(boolean ascending) {
        this.ascending = ascending;
    }

    public String getClientNameHeader() {
        return clientNameHeader;
    }

    public void setClientNameHeader(String clientNameHeader) {
        this.clientNameHeader = clientNameHeader;
    }

    public HtmlPanelGrid getContainerSearch() {
        return containerSearch;
    }

    public void setContainerSearch(HtmlPanelGrid containerSearch) {
        this.containerSearch = containerSearch;
    }

    public String getIdNumberHeader() {
        return idNumberHeader;
    }

    public void setIdNumberHeader(String idNumberHeader) {
        this.idNumberHeader = idNumberHeader;
    }

    public boolean isMultiRowSelect() {
        return multiRowSelect;
    }

    public void setMultiRowSelect(boolean multiRowSelect) {
        this.multiRowSelect = multiRowSelect;
    }

    public String getOldSort() {
        return oldSort;
    }

    public void setOldSort(String oldSort) {
        this.oldSort = oldSort;
    }

    public boolean isOldAscending() {
        return oldAscending;
    }

    public void setOldAscending(boolean oldAscending) {
        this.oldAscending = oldAscending;
    }

    public String getSortColumnName() {
        return sortColumnName;
    }

    public void setSortColumnName(String sortColumnName) {
        this.sortColumnName = sortColumnName;
    }

    public String getSurnameHeader() {
        return surnameHeader;
    }

    public void setSurnameHeader(String surnameHeader) {
        this.surnameHeader = surnameHeader;
    }

    public HtmlInputText getInputTextFilterByNSP() {
        return inputTextFilterByNSP;
    }

    public void setInputTextFilterByNSP(HtmlInputText inputTextFilterByNSP) {
        this.inputTextFilterByNSP = inputTextFilterByNSP;
    }

    public HtmlInputText getInputTextMail() {
        return inputTextMail;
    }

    public void setInputTextMail(HtmlInputText inputTextMail) {
        this.inputTextMail = inputTextMail;
    }

    public HtmlInputText getInputTextPhone() {
        return inputTextPhone;
    }

    public void setInputTextPhone(HtmlInputText inputTextPhone) {
        this.inputTextPhone = inputTextPhone;
    }

    public HtmlOutputLabel getLabelMail() {
        return labelMail;
    }

    public void setLabelMail(HtmlOutputLabel labelMail) {
        this.labelMail = labelMail;
    }

    public HtmlOutputLabel getLabelPhone() {
        return labelPhone;
    }

    public void setLabelPhone(HtmlOutputLabel labelPhone) {
        this.labelPhone = labelPhone;
    }

    public HtmlOutputLabel getLabelMenuSeleccion() {
        return labelMenuSeleccion;
    }

    public void setLabelMenuSeleccion(HtmlOutputLabel labelMenuSeleccion) {
        this.labelMenuSeleccion = labelMenuSeleccion;
    }

    public PanelStack getPanelStackMainMenu() {
        return panelStackMainMenu;
    }

    public void setPanelStackMainMenu(PanelStack panelStackMainMenu) {
        this.panelStackMainMenu = panelStackMainMenu;
    }

    public HtmlOutputLabel getLabelNSP() {
        return labelNSP;
    }

    public void setLabelNSP(HtmlOutputLabel labelNSP) {
        this.labelNSP = labelNSP;
    }

    public boolean isNspView() {
        return nspView;
    }

    public void setNspView(boolean nspView) {
        this.nspView = nspView;
    }

    
}
